package com.hh.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.basic.util.PageList;
import com.hh.org.domain.SdDetail;
import com.hh.org.domain.SdType;
import com.hh.org.mapper.SdDetailMapper;
import com.hh.org.mapper.SdTypeMapper;
import com.hh.org.query.SdTypeQuery;
import com.hh.org.service.ISdTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 35278
* @createDate 2022-06-09 19:24:09
*/
@Service
public class SdTypeServiceImpl  extends ServiceImpl<SdTypeMapper, SdType> implements ISdTypeService {
    @Resource
    private SdTypeMapper sdTypeMapper;

    @Resource
    private SdDetailMapper sdDetailMapper;

    /**
     * 高级＋分页查询
     * @param sdTypeQuery
     * @return
     */
    @Override
    public PageList<SdType> queryPage(SdTypeQuery sdTypeQuery) {
        Page<SdType> sdTypePage = new Page<>(sdTypeQuery.getCurrentPage(), sdTypeQuery.getPageSize());
//        Page<SdType> page = new LambdaQueryChainWrapper<>(sdTypeMapper)
//                .like(SdType::getSn, sdTypeQuery.getKeyword())
//                .like(SdType::getName, sdTypeQuery.getKeyword())
//                .page(sdTypePage);
        QueryWrapper<SdType> queryWrapper = new QueryWrapper<SdType>().and(wrapper -> wrapper.like("sn", sdTypeQuery.getKeyword()).or().like("name", sdTypeQuery.getKeyword()));
        Page<SdType> page = sdTypeMapper.selectPage(sdTypePage, queryWrapper);
        Integer total = (int)page.getTotal();
        List<SdType> list = page.getRecords();
        return new PageList<>(total,list);
    }

    /**
     * 删除类型时，将详情也删了
     * @param id
     */
    @Override
    public void myDelete(Long id) {
        sdDetailMapper.delete(new QueryWrapper<SdDetail>().eq("types_id",id));
        sdTypeMapper.deleteById(id);
    }
}




